<?php
/**
 * 参数列表模型
 * 
 * @version $Id: Params.php 458 2012-10-09 03:12:43Z linsir123 $
 */
class modelParams
{
	/**
	 * @var 参数列表
	 */
	public $data = array();
	
	/**
	 * @var 对应的淘宝客参数
	 */
	public $taobaoke;
	
	/**
	 * @var 排序说明
	 */
	private $_sorts;
	
	/**
	 * 构造函数
	 */
	public function __construct()
	{
		$keys = array(
			'p' => 'intval',
			'cid' => 'intval',
			'sort' => 'intval',
			'keyword' => 'trim',
			'start_price' => 'floatval',
			'end_price' => 'floatval',
			
			///
			'type' => 'trim',
			'pinyin' => 'trim',
		);
		foreach ($keys as $k => $m) {
			$v = call_user_func_array($m, array($_GET[$k]));
			if ( ! empty($v)) 
				$this->data[$k] = $v;
		}
		
		///
		$this->_init();
		$this->_initTobaoke();
	}
	
	/**
	 * 
	 */
	public function getP()
	{
		return isset($this->data['p']) ? $this->data['p'] : 1;
	}
	
	/**
	 * 
	 */
	public function getSort()
	{
		return isset($this->data['sort']) ? $this->data['sort'] : 0;
	}
	
	/**
	 * 
	 */
	public function getCid()
	{
		return isset($this->data['cid']) ? $this->data['cid'] : 0;
	}
	
	/**
	 * 
	 */
	public function getType()
	{
		return isset($this->data['type']) ? $this->data['type'] : 'list';
	}
	
	/**
	 * 
	 */
	public function getPinyin()
	{
		return isset($this->data['pinyin']) ? $this->data['pinyin'] : '';
	}
	
	/**
	 * 
	 */
	public function getKeyword()
	{
		return isset($this->data['keyword']) ? $this->data['keyword'] : '';
	}
	
	/**
	 * 构造查询链接
	 */
	public function createUrl($params = array())
	{
		if (empty($params)) 
			$params = $this->data;
			
		///
		return Route::urlClass($params);
	}
	
	/**
	 * 内部重定向
	 */
	public function h301($data = array())
	{
		$this->data = array_merge($this->data, $data);
		$url = $this->createUrl();
		
		///
		lpSystem::h301($url);
	}
	
	/**
	 * 转换到视图层
	 */
	public function assign($totalPage, $category = null)
	{
		$title = null;
		$keyMeta = array();
		$list = M::nav()->locations;
		for ($i=count($list)-1; $i>=0; $i--) {
			$tmp = $list[$i]['title'];
			$keyMeta[] = $tmp;
			$title = $tmp.(empty($title)?'':'_').$title;
		}
			
		///
		if ($keyword = $this->getKeyword()) {
			$tmp = htmlspecialchars($keyword);
			$keyMeta[] = $tmp;
			$title = $tmp.(empty($title)?'':'_').$title;
		}
			
		///
		$p = $this->getP();
		if ($p > 1) 
			$title .= '(第'.$p.'页)';
			
		///
		View::assignSet(array(
			'sortInfo' => $this->_aSortInfo(), 
			'pagination' => $this->_aPagination($totalPage),
			'keyMeta' => implode(',', $keyMeta),
			'keyword' => $keyword, 
			'title' => $title, 
		));
	}
	
	
	/// 以下为私有方法 ///
	
	
	/**
	 * 初如化排序信息
	 */
	private function _init()
	{
		$pinyin = $this->getPinyin();
		$keyword = $this->getKeyword();
		$type = $this->getType();
		$cId = $this->getCid();
		switch ($type) {
			case 'zhe':
				$this->data['show'] = 20;
				$this->_sorts = array(
					array('默认',	'default'),
					array('折扣价',	'price_asc'),
					array('信用',	'credit_desc'),
					array('销量',	'volume_desc'),
					array('返现比例',	'commissionRate_desc'),
				);
				
				/// 跳转
				if ( ! empty($cId) || empty($keyword)) 
					$this->h301(array(
						'keyword'=>empty($keyword)?'女装':$keyword,
						'pinyin'=>'search',
						'cid'=>0
					));
					
				break;
				
			default:
				$this->data['show'] = 21;
				$this->_sorts = array(
					array('默认',	'default'),
					array('价格',	'price_asc'),
					array('信用',	'credit_desc'),
					array('销量',	'commissionNum_desc'),
					array('热销',	'commissionVolume_desc'),
					array('返现比例',	'commissionRate_desc'),
				);
				
				/// 跳转
				if (empty($cId) && empty($keyword)) 
					$this->h301(array('cid'=>16));
					
				if (empty($cId) && ! empty($keyword) && $pinyin != 'search') 
					$this->h301(array('pinyin'=>'search'));
					
				break;
		}
		
		///
		$key = $this->getSort();
		if ($key >= count($this->_sorts)) 
			$this->h301(array('sort'=>0));
	}
	
	/**
	 * 初始化淘宝客参数
	 */
	private function _initTobaoke()
	{
		$keys = array('show', 'p', 'cid', 'keyword', 'start_price', 'end_price');
		foreach ($keys as $k) {
			if (isset($this->data[$k])) 
				$params[$k] = $this->data[$k];
		}
		
		///
		$key = $this->getSort();
		$params['sort'] = $this->_sorts[$key][1];
		$this->taobaoke = $params;
	}
	
	/**
	 * 排序信息
	 */
	private function _aSortInfo()
	{
		$params = $this->data;
		$params['p'] = 1;
		$params['sort'] = '__SORT__';
		$format = $this->createUrl($params);
		
		///
		$tmp = '';
		foreach ($this->_sorts as $i => $v) {
			if ($this->data['sort'] == $i) {
				$class = 'class="this"';
				$href = '#';
			} else {
				$class = '';
				$href = str_replace('__SORT__', $i, $format);
			}
			$tmp .= '<li '.$class.'><a href="'.$href.'">'.$v[0].'</a></li>';
		}
		
		///
		return $tmp;
	}
	
	/**
	 * 分页
	 */
	private function _aPagination($totalPage)
	{
		$params = $this->data;
		$params['p'] = '__PAGE__';
		$url = $this->createUrl($params);
		$com = App::C('Pagination', $url, $this->getP(), $totalPage, 8, array(
			'pre' =>'上一页', 
			'next' =>'下一页'));
			
		///
		$com->setUrlPStyle(1);
		return $com->display();
	}
}
